package com.bbgwap.controller;

import com.alibaba.fastjson.JSON;
import com.bbgkh.model.BaseInfo;
import com.bbgkh.model.DTO.MemberInfoDTO;
import com.bbgkh.service.IMemberService;
import com.bbgkh.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by laolijia on 2017/4/23.
 */
@Controller
public class MemberController extends BaseController{


    @Autowired
    private IMemberService memberService;


    @PostMapping(value = "member/newMemberInfo")
    @ResponseBody
    public String newSaleInfo(@RequestBody MemberInfoDTO memberInfoDTO, HttpServletRequest request, HttpServletResponse response){
        BaseInfo baseInfo = null;
        String uid = UserUtils.getUid(request);
        memberInfoDTO.setUid(uid);
        try {
            baseInfo= memberService.addMemberInfo(memberInfoDTO);
        } catch (Exception e) {
            logger.error("插入会员数据失败",e);

            baseInfo= new BaseInfo("100","插入会员数据失败");

        }

        return JSON.toJSONString(baseInfo);
    }

    @GetMapping(value = "member/myMemberPage")
    public String myMemberPage( HttpServletRequest request, HttpServletResponse response){
        return "member/myMemberPage";
    }

    @GetMapping(value = "member/queryMemberInfo/{memberMobile}")
    @ResponseBody
    public String queryMemberInfo( @PathVariable String memberMobile,
                                   HttpServletRequest request, HttpServletResponse response){

        BaseInfo baseInfo =null;
        MemberInfoDTO memberInfoDTO = new MemberInfoDTO();
        if(memberMobile.length()==11){
            //对传入的memberMobile进行判读，如果是手机号，则用手机号查询，其他的用名字查询
            memberInfoDTO.setMobilePhone(memberMobile);
        }else {
            memberInfoDTO.setMemberName(memberMobile);
        }

        baseInfo = memberService.queryMemberInfo(memberInfoDTO);

        return JSON.toJSONString(baseInfo);
    }
}
